/**
 * 收货地址管理
 */
var v_receiver_edit = new Vue({
	el: '#receiverEditPage',
	data: {
		title: '收货地址编辑',
		needBack: true,
		needRefresh: false,
		show: false,
		form: {
			receiver: '',
			phone: '',
			province: '四川',
			city: '成都',
			region: '锦江区',
			address: ''
		}
	},
	updated: function() {
		$("#cityPicker").cityPicker({
			toolbarTemplate: '<header class="bar bar-nav">\
			<button class="button button-link pull-right close-picker">确定</button>\
			<h1 class="title">选择收货地址</h1>\
			</header>',
			value: [v_receiver_edit.form.province, v_receiver_edit.form.city, v_receiver_edit.form.region],
			onClose: function() {
				var adds = $("#cityPicker").val().split(' ');
				v_receiver_edit.form.province = adds[0];
				v_receiver_edit.form.city = adds[1];
				v_receiver_edit.form.region = adds[2];
			},
			onOpen: function(a) {
				console.log(a);
			}
		});
	},
	methods: {
		goback: function() {
			// 返回
		},
		postReceiver: function() {
			axios.post('/order/reveiver/save', this.form)
			.then(function(resp) {
				$.toast(resp.data.message);
				if (resp.data.success) {
					$.router.back();
					v_receiver.initReceivers(v_receiver_edit.form.agentId);
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		}
	}
});
var v_receiver = new Vue({
	el: '#receiverPage',
	data: {
		title: '收货地址管理',
		needBack: true,
		needRefresh: false,
		show: false,
		navs: [{
			title: '新增',
			icon: 'icon-add1',
			url: '#',
			active: '',
			badge: 0,
			option: function() {
				v_receiver_edit.show = true;
				v_receiver_edit.form = {
						receiver: '',
						phone: '',
						province: '四川',
						city: '成都',
						region: '锦江区',
						address: ''
					};
				v_receiver_edit.$nextTick(function() {
					$.router.load('#receiverEditPage');  //加载内联页面
				});
			}
		}],
		receivers: []
	},
	methods: {
		init: function() {
			this.initReceivers();
		},
		goback: function() {
			// 返回
		},
		checkReceiver: function(receiver) {
			$.each(this.receivers, function(i, re) {
				re.checked = false;
			});
			receiver.checked = true;
		},
		setDefault: function(receiver) {
			receiver.isDefault = true;
			axios.post('/order/reveiver/setDefault/' + receiver.id)
			.then(function(resp) {
				if (resp.data.success) {
					$.toast('已设置' + receiver.receiver + '为默认地址');
					$.each(v_receiver.receivers, function(i, re) {
						if (re.id != receiver.id) {
							re.isDefault = false;
						}
					});
					v_receiver.$forceUpdate();
				} else {
					$.toast(resp.data.message);
				}
			})
			.catch(function (error) {
				console.log(error);
			});
			
		},
		editReceiver: function(receiver) {
			v_receiver_edit.show = true;
			v_receiver_edit.form = receiver;
			v_receiver_edit.$nextTick(function() {
				$.router.load('#receiverEditPage');  //加载内联页面
			});
		},
		removeReceiver: function(receiver) {
			$.confirm('你确定要删除'+receiver.receiver+'的收货信息吗？', function () {
				axios.post('/order/reveiver/delete/' + receiver.id)
				.then(function(resp) {
					$.toast(resp.data.message);
					if (resp.data.success) {
				    	v_receiver.initReceivers();
					}
				})
				.catch(function (error) {
				    console.log(error);
				});
			});
		},
		goBackPage: function(receiver) {
			
		},
		initReceivers: function(agentId, receiverId) {
			axios.post('/order/reveiver/current/list')
			.then(function(resp) {
				if (resp.data.success) {
					v_receiver.receivers = $.map(resp.data.rows, function(row) {
						if (receiverId && row.id == receiverId) {
							row.checked = true;
						}
						return row;
					});
					v_receiver.$nextTick(function() {
						$(v_receiver.$el).find('input[type=radio][value="'+receiverId+'"]').prop('checked', true);
					});
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		}
	}
});